package com.lfz.demo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lfz.demo.entity.Logs;
import com.lfz.demo.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import java.util.List;
import java.util.Map;

/**
 * @author lfz
 * @date 2025/3/3 9:33
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 登录时，查询数据库，验证账号密码
     * @param username
     * @param password
     * @return
     */
    @Select("select * from user where username = #{username} and password = #{password}")
    User login(String username, String password);

    @Select("select a.username,b.name FROM user as a where a.username = #{username} left join ban_ji as b on a.ban_ji_id = b.id limit #{page},#{limit}")
    void loadUserByLeftJoin(String username, Integer page, Integer limit);

    /**
     * 新注册用户默认分配权限为普通用户
     * 先注册保存用户后，查询最新的用户id，再为该id插入user_role普通用户权限rid=4
     */
    @Insert("insert into user_role(uid,rid) values ((select id from user order by id desc limit 1),4)")
    void AssignPermissionsByLoadUserIdDescOne();

    /**
     * 验证当前找回密码用户是否存在，统计是否有对应的username
     */
    @Select("SELECT COUNT(*) FROM user WHERE username = #{username}")
    int countByUsername(@Param("username") String username);

    /**
     * 通过username来查对应的手机号
     */
    @Select("SELECT phone FROM user WHERE username = #{username}")
    String phoneByUsername(@Param("username") String username);

    /**
     * 通过username来查对应的信息
     */
    @Select("SELECT name,id_card as idCard,address FROM user WHERE username = #{username}")
    Map<String, String> infoByUsername(String username); // 返回 Map 存储多个字段
}
